﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace eventr.View.Util
{
    public class RefinerValueFormatter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string val = value as string;
            if (val != null)
            {
                if (val.StartsWith("From ", StringComparison.InvariantCultureIgnoreCase)
                    || val.StartsWith("Before ", StringComparison.InvariantCultureIgnoreCase)
                    || val.EndsWith("or Later", StringComparison.InvariantCultureIgnoreCase))
                    return ConvertDateRangetoPrettyString(val);
                else
                    return val;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        private string ConvertDateRangetoPrettyString(string input)
        {
            string[] parts = input.Split(' ');

            if(input.StartsWith("From ", StringComparison.InvariantCultureIgnoreCase))
            {
                DateTime start = DateTime.Parse(parts[1]);
                DateTime end = DateTime.Parse(parts[3]);
                return string.Format("{0} - {1}", start.ToString(), end.ToString());
            }
            else if(input.StartsWith("Before ", StringComparison.InvariantCultureIgnoreCase))
            {
                DateTime end = DateTime.Parse(parts[1]);
                return string.Format("Before {0}", end.ToString());
            }

            else if(input.EndsWith(" Or Later ", StringComparison.InvariantCultureIgnoreCase))
            {
                DateTime start = DateTime.Parse(parts[0]);
                return string.Format("After {0}", start.ToString());
            }

            return null;
        }
    }
}
